package com.jiamao.goplay.adapter;

import android.content.Context;
import android.os.Build;
import android.text.Html;
import android.text.Spanned;
import android.view.LayoutInflater;
import android.view.ViewGroup;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.itheima.roundedimageview.RoundedImageView;
import com.jiamao.goplay.R;
import com.jiamao.goplay.ui.discover.vm.CityMenuItemVM;
import com.jiamao.goplay.ui.discover.vm.NeayByItemVM;
import com.jiamao.goplay.ui.discover.vm.SearchItemVM;
import com.jiamao.goplay.view.PileLayout;

import androidx.annotation.LayoutRes;
import androidx.annotation.RequiresApi;
import androidx.databinding.ViewDataBinding;
import me.tatarka.bindingcollectionadapter2.BindingRecyclerViewAdapter;

public class SearchAdapter extends BindingRecyclerViewAdapter {

    Context context;
    LayoutInflater inflater;
    private String key;
    public SearchAdapter(Context context){
        this.context = context;
         inflater = LayoutInflater.from(context);
    }

    @Override
    public ViewDataBinding onCreateBinding(LayoutInflater inflater, @LayoutRes int layoutId, ViewGroup viewGroup) {
        ViewDataBinding binding = super.onCreateBinding(inflater, layoutId, viewGroup);
        return binding;
    }

    private int iondex = 0 ;

    @RequiresApi(api = Build.VERSION_CODES.M)
    @Override
    public void onBindBinding(ViewDataBinding binding, int bindingVariable, @LayoutRes int layoutId, int position, Object item) {
        super.onBindBinding(binding, bindingVariable, layoutId, position, item);
                if (item instanceof SearchItemVM){

                    SearchItemVM vm  = (SearchItemVM) item;

                    TextView tvName = binding.getRoot().findViewById(R.id.tv_name);
                    if(vm.name.get().contains(key)){
                        int index=vm.name.get().indexOf(key);
                        int len=key.length();
                        Spanned temp= Html.fromHtml(vm.name.get().substring(0,index)+"<font color=#ff0000>"
                                +vm.name.get().substring(index,index+len)+"</font>"
                                +vm.name.get().substring(index+len,vm.name.get().length()));
                        tvName.setText(temp);

                    }else {
                       // textView1.setText(mlist.get(position).getKnowledge());
                    }

             }
    }

    public void setKey(String  key){
        this.key = key;


    }

//    @Override
//    public void onBindBinding(@NonNull ViewDataBinding binding, int variableId, int layoutRes, int position, Object item) {
//        super.onBindBinding(binding, variableId, layoutRes, position, item);
//
//        if (item instanceof CityMenuItemVM){
//            CityMenuItemVM vm  = (CityMenuItemVM) item;
//
//        }
//
//    }
}
